const path = require('path');
const DotenvPlugin = require('dotenv-webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { PUBLIC_PATH, DIST_PATH, ENV_CONFIG_PATH, IS_DEV, SRC_PATH } = require('./variable');

/**
 * 获取 getHTMLPlugins
 */
const getHTMLPlugins = () => {
  const indexHtmlPlugin = new HtmlWebpackPlugin({
      template: path.join(PUBLIC_PATH, 'index.html'),
      filename: 'index.html',
      //true 插入body底部，head:插入head标签，false:不生成js文件
      inject: true,
      // 会在打包好的bundle.js后面加上hash串
      hash: true,
      title: '',
      minify: {
          // 删除注释
          removeComments: true,
          collapseWhitespace: true,
          // 压缩 HTML 中出现的 CSS 代码
          minifyCSS: true,
          // 压缩 HTML 中出现的 JS 代码
          minifyJS: true,
      }
  });
  return [indexHtmlPlugin];
}

/**
 * 获取 插件
 */
const getPlugins = () => {

    // 使用 dotenv https://www.jianshu.com/p/a76f15870a73
    const dotenvPlugin = new DotenvPlugin({
        path: ENV_CONFIG_PATH,
    });

    return [
        ...getHTMLPlugins(),
        dotenvPlugin,
    ];
};

module.exports = {
    getPlugins,
}